package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车业务实现类
 *
 * @author admin
 * @date 2024/10/4
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //     查询购物车商品
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);

        if (cartList != null && cartList.size() > 0) {
            //     重复，数量加1
            ShoppingCart shop = cartList.get(0);
            shop.setNumber(shop.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shop);
        } else {

            //     判断是套餐还是菜品
            if (shoppingCart.getDishId() != null) {
                // 菜品
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                BeanUtils.copyProperties(dish, shoppingCart, "id");
                shoppingCart.setAmount(dish.getPrice());
                // bigDecimal 不能直接赋值，需要计算，乘法运算
            } else {
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                BeanUtils.copyProperties(setmeal, shoppingCart, "id");
                shoppingCart.setAmount(setmeal.getPrice());
            }

            //     加入购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }
    }


    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build());
    }


    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
    //     查找选中商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);

        if (cartList == null || cartList.size() == 0) {
            throw new DeletionNotAllowedException(MessageConstant.CART_IS_NULL);
        }
        ShoppingCart cart = cartList.get(0);

        if (cart.getNumber() == 1) {
            // 数量=1，删除商品
            shoppingCartMapper.delete(List.of(cart.getId()));
        } else {
            // 数量减一
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(cart);
        }
    }


    /**
     * 清空购物车
     */
    @Override
    public void clean() {
    // //     查询购物车所有商品id
    //     List<ShoppingCart> cartList = shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    //     if(cartList == null || cartList.size() == 0) {
    //         throw new DeletionNotAllowedException(MessageConstant.CART_IS_NULL);
    //     }
    //     List<Long> ids = cartList.stream().map(ShoppingCart::getId).collect(Collectors.toList());
    //     shoppingCartMapper.delete(ids);

    //     根据用户id删除
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
